package net.controllers;

import java.net.BindException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.model.GridAndBackground;
import net.service.GridAndBackgroundManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/separationOfObjects")
public class SeparationOfObjectsController {
	@Autowired
	private GridAndBackgroundManager gridAndBackgrounManagerImpl;

	@RequestMapping(method = RequestMethod.GET)
	public String getUploadForm(Model model,HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		model.addAttribute(gridAndBackgrounManagerImpl.getGridAndBackground());
		session.setAttribute("mode",gridAndBackgrounManagerImpl.getGridAndBackground().getSeparationOfObjects());
		return "/separationofobjects";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String create(GridAndBackground gridAndBackground,
			HttpServletRequest request, HttpServletResponse response,
			Object command, BindException errors, HttpSession session) {
		boolean separationOfObjects ;
		try {
			separationOfObjects = getMode(request);
			gridAndBackgrounManagerImpl
			.setSeparationOfObjects(separationOfObjects);
		} catch (Exception e) {
			System.out.println("Error: Determinar si se separan o no los objetos.");
			return "redirect:/forms/gridReference";
		}
		
			
		
		return "redirect:/forms/home";
	}
	
	private boolean getMode(HttpServletRequest request) {
		boolean mode=true;
		if (request.getParameter("mode") != "") {
			mode =Boolean.parseBoolean(request.getParameter("mode"));
		}
		return mode;
	}

}
